package com.qiniu.githubdata.githubSpider;

import java.io.IOException;
import java.util.Map;

import com.qiniu.githubdata.job.GitHubPageScraper;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.kohsuke.github.GHRepository;
import org.kohsuke.github.GHUser;
import org.kohsuke.github.GitHub;
import org.kohsuke.github.GitHubBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import com.qiniu.githubdata.job.GitHubRepoFetcher;
import org.springframework.test.context.junit.jupiter.SpringExtension;

import javax.annotation.Resource;

@SpringBootTest
@ExtendWith(SpringExtension.class)
public class scrapeData {
    @Autowired
    private GitHubRepoFetcher gitHubRepoFetcher;
    @Resource
    private GitHubPageScraper gitHubPageScraper;
    GitHub github = new GitHubBuilder().
            withOAuthToken("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").build();

    public scrapeData() throws IOException {
    }

    //    Git
    @Test
    public void testScrapeData() {
        try {
            gitHubRepoFetcher.storeRepoDataByOwner("alibaba");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void contributionTest() {
        try {
            Map<String, GHRepository> repos = gitHubRepoFetcher.getReposByOwner("alibaba");
            gitHubRepoFetcher.getContributions(repos.get("EasyNLP"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void storeUser(){
        try {

            GHUser alibaba = github.getUser("alibaba");
            gitHubRepoFetcher.storeDeveloperData(alibaba);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    @Test
    public void storeAllData(){
        github.searchRepositories().q("stars:>10000").list().forEach(repository -> {
            try {
                gitHubRepoFetcher.storeRepoData(repository);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            String owner = repository.getOwnerName();
            try {
                gitHubRepoFetcher.storeDeveloperData(repository.getOwner());
                gitHubRepoFetcher.getReposByOwner(owner).forEach((s, ghRepository) -> {
                    try {
                        gitHubRepoFetcher.storeRepoData(ghRepository);
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                    try {
                        gitHubRepoFetcher.getContributions(ghRepository);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }
    @Test
    public void followerTest() throws IOException {
//        try {
//            gitHubRepoFetcher.getUserFollowers("alibaba");
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        GHUser alibaba = github.getUser("TommyZihao");
//        int followerCount = alibaba.getFollowersCount();
        gitHubPageScraper.getIndirectFollowers(alibaba);
//        gitHubPageScraper.getAndSaveFollowersByPage(alibaba,1);

    }

    @Test
    public void repoPageTest() throws IOException {
        GHUser user = github.getUser("TommyZihao");
        gitHubPageScraper.getAndSaveReposByPageWithOwnerName(user,1);
    }
    @Test
    public void nationCountTest() throws IOException {

    }
    @Test
    public void searchUserAndStore() {
        gitHubRepoFetcher.searchUser();
    }
}
